
import java.io.InputStream;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class FileBrowser implements CommandListener {

    private static final String UP_DIRECTORY = "..";
    private static final String MEGA_ROOT = "/";
    private static final String SEP_STR = "/";
    private static final char SEP = '/';
    private String currDirName;
    private Command back = new Command("Back", Command.BACK, 2);
    private Command cmdSend = new Command("Send", Command.OK, 2);
    private UpAnh upAnh;
    private Image dirIcon;
    private Image fileIcon;
    private byte[] datas;
    private int type;

    public FileBrowser(UpAnh upAnh, int type) {
        this.upAnh = upAnh;
        this.type = type;
        currDirName = MEGA_ROOT;
        try {
            dirIcon = Image.createImage("/dir.png");
        } catch (Exception e) {
            dirIcon = null;
        }
        try {
            fileIcon = Image.createImage("/file.png");
        } catch (Exception e) {
            fileIcon = null;
        }
    }

    public void ShowCurrDir() {
        Enumeration e;
        FileConnection currDir = null;
        List browser;
        try {
            if (MEGA_ROOT.equals(currDirName)) {
                e = FileSystemRegistry.listRoots();
                browser = new List(currDirName, List.IMPLICIT);
            } else {
                currDir = (FileConnection) Connector.open("file://localhost/" + currDirName);
                e = currDir.list();
                browser = new List(currDirName, List.IMPLICIT);
                browser.append(UP_DIRECTORY, dirIcon);
            }
            while (e.hasMoreElements()) {
                String fileName = (String) e.nextElement();
                if (fileName.charAt(fileName.length() - 1) == SEP) {
                    browser.append(fileName, dirIcon);
                } else {
                    browser.append(fileName, fileIcon);
                }
            }
            browser.setCommandListener(this);
            browser.addCommand(back);
            if (currDir != null) {
                currDir.close();
            }
            upAnh.display.setCurrent(browser);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == back) {
            if (currDirName.equals(MEGA_ROOT)) {
                if (type == 0) {
                    upAnh.DisplayViewImage();
                } else {
                    upAnh.DisplayUpAnhChose();
                }
            } else {
                ShowCurrDir();
            }
        } else if (c == List.SELECT_COMMAND) {
            List browser = (List) d;
            final String currFile = browser.getString(browser.getSelectedIndex());
            new Thread(new Runnable() {

                public void run() {
                    if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY)) {
                        TraverseDirectory(currFile);
                    } else {
                        try {
                            datas = ReadFile("file://localhost/" + currDirName + currFile);
                            ViewImage(datas);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }).start();
        } else if (c == cmdSend) {
            try {
                upAnh.DisplayUpAnhForm(datas, false);
//                upAnh.client.SendImage("",datas);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static byte[] ReadFile(String fileName) throws Exception {
        FileConnection fc = (FileConnection) Connector.open(fileName, Connector.READ);
        if (fc.exists()) {
            InputStream is = fc.openInputStream();
            byte[] data = new byte[(int) fc.fileSize()];
            int read = 0;
            while (read != -1) {
                read = is.read(data);
            }
            return data;
        }
        return null;
    }

    private void ViewImage(byte[] datas) {
        ImageLocal viewImage = new ImageLocal(datas);
        viewImage.addCommand(back);
        if (type == 1) {
            viewImage.addCommand(cmdSend);
        }
        viewImage.setCommandListener(this);
        upAnh.display.setCurrent(viewImage);
    }

    public void TraverseDirectory(String fileName) {
        if (currDirName.equals(MEGA_ROOT)) {
            if (fileName.equals(UP_DIRECTORY)) {
                return;
            }
            currDirName = fileName;
        } else if (fileName.equals(UP_DIRECTORY)) {
            int i = currDirName.lastIndexOf(SEP, currDirName.length() - 2);
            if (i != -1) {
                currDirName = currDirName.substring(0, i + 1);
            } else {
                currDirName = MEGA_ROOT;
            }
        } else {
            currDirName = currDirName + fileName;
        }
        ShowCurrDir();
    }

    private class ImageLocal extends Canvas {

        private Image image;

        public ImageLocal(byte[] datas) {
            try {
                image = Image.createImage(datas, 0, datas.length);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        protected void paint(Graphics g) {
            int x = 0;
            int y = 0;
            if (image.getWidth() < getWidth()) {
                x = (getWidth() - image.getWidth()) / 2;
            }
            if (image.getHeight() < getHeight()) {
                y = (getHeight() - image.getHeight()) / 2;
            }
            g.drawImage(image, x, y, 0);
        }
    }
}
